package com.baiqian.doupo.item;

import com.baiqian.doupo.Utils;
import com.baiqian.doupo.modgroup.DouPoModGroup;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.apache.logging.log4j.LogManager;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

public abstract class YaoCai extends Item {
    public YaoCai() {
        super(new Properties().group(DouPoModGroup.douPoYaoCaiGroup));
    }

    /**将List代表的物品塞入nbt*/
    public static void getNbtFromList(List<ItemStack> list, CompoundNBT compoundNBT, int list_num, List<Integer> making_tick, List<Double> making_point){
        compoundNBT.putInt("YaoCai"+list_num+":"+"num",list.size());
        for(int i=0;i<list.size();++i){
            Item item = list.get(i).getItem();
            if(ZiYeLanCao.class.equals(item.getClass())){
                compoundNBT.putInt("YaoCai"+list_num+":"+i,1);
            }
            else if(XiGuHua.class.equals(item.getClass())){
                compoundNBT.putInt("YaoCai"+list_num+":"+i,2);
            }
            if(list_num==1){
                try {
                    compoundNBT.getInt("YaoCai" + list_num + ":" + i);
                    compoundNBT.putInt("YaoCai" + list_num + ":" + i + ":" + "making_tick", making_tick.get(i));
                    compoundNBT.putDouble("YaoCai" + list_num + ":" + i + ":" + "making_point", making_point.get(i));
                } catch (Exception e) {
                    LogManager.getLogger().info("noYaoCai");
                }
            }
        }
    }

    /**将nbt的物品录入List*/
    public static List<ItemStack> getListFromNbt(CompoundNBT compoundNBT,int list_num, List<Integer> making_tick, List<Double> making_point){
        List<ItemStack> itemStacks = new ArrayList<>();
        int size = compoundNBT.getInt("YaoCai"+list_num+":"+"num");
        for(int i=0;i<size;++i){
            int num = compoundNBT.getInt("YaoCai"+list_num+":"+i);
            boolean flag = true;
            switch (num){
                case 1:itemStacks.add(new ItemStack(ItemRegistry.zi_ye_lan_cao.get()));break;
                case 2:itemStacks.add(new ItemStack(ItemRegistry.xi_gu_hua.get()));break;
                default:flag=false;
            }
            if(flag&&list_num==1){
                making_tick.add(compoundNBT.getInt("YaoCai"+list_num+":"+i+":"+"making_tick"));
                making_point.add(compoundNBT.getDouble("YaoCai"+list_num+":"+i+":"+"making_point"));
            }
        }
        return itemStacks;
    }

    /**获取激活温度*/
    public abstract double getBeActivateTemprature();
    /**获取销毁温度*/
    public abstract double getBeRemoveTemprature();
    /**获取最大激活时间tick*/
    public abstract double getBeActivateMaxTick();
    /**获取激活点数*/
    public abstract double getBeActivatePoint();
    public YaoCai(Properties properties) {
        super(properties);
    }

    @OnlyIn(Dist.CLIENT)
    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn){
        tooltip.add(ITextComponent.getTextComponentOrEmpty(new TranslationTextComponent("item." + Utils.MOD_ID + ".yao_cai.1").getString() + String.format("%.0f", getBeActivateTemprature())));
        tooltip.add(ITextComponent.getTextComponentOrEmpty(new TranslationTextComponent("item." + Utils.MOD_ID + ".yao_cai.2").getString() + String.format("%.0f", getBeRemoveTemprature())));
        tooltip.add(ITextComponent.getTextComponentOrEmpty(new TranslationTextComponent("item." + Utils.MOD_ID + ".yao_cai.3").getString() + String.format("%.0f", getBeActivateMaxTick() / 20.0)));
        tooltip.add(ITextComponent.getTextComponentOrEmpty(new TranslationTextComponent("item." + Utils.MOD_ID + ".yao_cai.4").getString() + String.format("%.0f", getBeActivatePoint())));
    }
}
class ZiYeLanCao extends YaoCai{
    /**
     * 被炼化温度
     */
    public final static double Be_Activate_Temprature = 140;
    /**
     * 被销毁温度
     */
    public final static double Be_Remove_Temprature = 500;
    /**
     * 允许炼化最大时间（耐久）
     */
    public final static double Be_Activate_Max_Tick = 30 * 20;
    /**
     * 炼化完毕需要的点数
     */
    public final static double Be_Activate_Point = 20 * 20;


    @Override
    public double getBeActivateTemprature() {
        return Be_Activate_Temprature;
    }

    @Override
    public double getBeRemoveTemprature() {
        return Be_Remove_Temprature;
    }

    @Override
    public double getBeActivateMaxTick() {
        return Be_Activate_Max_Tick;
    }

    @Override
    public double getBeActivatePoint() {
        return Be_Activate_Point;
    }
}
class XiGuHua extends YaoCai{
    /**
     * 被炼化温度
     */
    public final static double Be_Activate_Temprature = 120;
    /**
     * 被销毁温度
     */
    public final static double Be_Remove_Temprature = 490;
    /**
     * 允许炼化最大时间（耐久）
     */
    public final static double Be_Activate_Max_Tick = 25 * 20;
    /**
     * 炼化难度
     */
    public final static double Be_Activate_Point = 30 * 20;

    @Override
    public double getBeActivateTemprature() {
        return Be_Activate_Temprature;
    }

    @Override
    public double getBeRemoveTemprature() {
        return Be_Remove_Temprature;
    }

    @Override
    public double getBeActivateMaxTick() {
        return Be_Activate_Max_Tick;
    }

    @Override
    public double getBeActivatePoint() {
        return Be_Activate_Point;
    }
}
